package com.gewu.pm.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.TelegraphicTransferBean;
import com.gewu.pm.ui.activity.filter.FilterWireTransferActivity;
import com.gewu.pm.ui.adapter.order.TelegraphicTransferAdapter;
import com.gewu.pm.ui.dialog.CustomTimeDialog;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.gewu.pm.widget.FilterBar;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.base.BasePopupWindow;
import com.hjq.widget.view.EmptyView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 电汇收款页面
 */
public final class TelegraphicTransferActivity extends AppActivity
        implements OnRefreshLoadMoreListener, BaseAdapter.OnItemClickListener {

    private SmartRefreshLayout srlRefresh;
    private RecyclerView rvTransferList;
    private FilterBar filterBar;
    private TextView tvRegister;
    private EmptyView emptyView;

    private TelegraphicTransferAdapter adapter;

    /**
     * 电汇收款列表
     */
    private List<TelegraphicTransferBean> transferList;

    public static void start(Context context) {
        Intent intent = new Intent(context, TelegraphicTransferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.telegraphic_transfer_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        srlRefresh = findViewById(R.id.srl_refresh);
        rvTransferList = findViewById(R.id.rv_transfer_list);
        filterBar = findViewById(R.id.filter_bar);
        tvRegister = findViewById(R.id.tv_register);
        emptyView = findViewById(R.id.empty_view);

        // 设置适配器
        adapter = new TelegraphicTransferAdapter(this);
        adapter.setOnItemClickListener(this);
        rvTransferList.setAdapter(adapter);

        // 设置刷新监听
        srlRefresh.setOnRefreshLoadMoreListener(this);
        
        // 设置 FilterBar 和其他按钮的点击事件
        setOnClickListener(filterBar.getDateFilterLayout(), tvRegister);
    }

    @Override
    protected void initData() {
        transferList = new ArrayList<>();
        // 设置默认时间筛选为"最近30天"
        filterBar.setDateFilterText("最近30天");
        
        // 初始化模拟数据
        loadMockData();
    }
    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == filterBar.getDateFilterLayout()) {//选择日期
            List<String> str = new ArrayList<>();
            str.add("今天");
            str.add("昨天");
            str.add("本月");
            str.add("上月");
            str.add("最近7天");
            str.add("最近30天");
            str.add("自定义时间");
            
            // 获取当前显示的文本，用于设置选中状态
            String currentText = filterBar.getDateFilterTextView().getText().toString();
            
            new SelectTimePopup.Builder(getContext())
                    .setList(str)
                    .setSelectedItem(currentText)  // 根据当前显示的文本设置选中项
                    .setListener(new SelectTimePopup.OnListener() {
                        @Override
                        public void onSelected(BasePopupWindow popupWindow, String category) {
                            if ("自定义时间".equals(category)) {
                                // 显示自定义时间选择对话框
                                showCustomTimeDialog();
                            } else {
                                toast(category);
                                // 更新 FilterBar 显示的日期文本
                                filterBar.setDateFilterText(category);
                            }
                        }
                    })
                    .showAsDropDown(filterBar.getDateFilterLayout());
        } else if (view == tvRegister) {
            // 跳转到收款登记页面
            PaymentRegistrationActivity.start(getContext());
        }
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        // TODO: 打开筛选页面
        //startActivity(FilterActivity.class);
        FilterWireTransferActivity.start(this);
    }
    /**
     * 显示自定义时间选择对话框
     */
    private void showCustomTimeDialog() {
        new CustomTimeDialog.Builder(getContext())
                .setListener(new CustomTimeDialog.OnListener() {
                    @Override
                    public void onSelected(BaseDialog dialog, int startYear, int startMonth, int startDay,
                                           int endYear, int endMonth, int endDay) {
                        // 格式化日期显示
                        String startDate = startYear + "-" + String.format("%02d", startMonth) + "-" + String.format("%02d", startDay);
                        String endDate = endYear + "-" + String.format("%02d", endMonth) + "-" + String.format("%02d", endDay);
                        String timeRange = startDate + " 至 " + endDate;

                        // 更新 FilterBar 显示的日期文本
                        filterBar.setDateFilterText(timeRange);
                        toast("已选择时间范围：" + timeRange);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        // 取消选择
                    }
                })
                .show();
    }
    /**
     * 加载模拟数据
     */
    private void loadMockData() {
        transferList.clear();

        transferList.add(new TelegraphicTransferBean(
                "终端名称展示在这里",
                "待审批",
                "霍辉",
                "2025-8-18 19:30",
                "FH202508130002",
                "电汇",
                "+¥40.00"
        ));

        transferList.add(new TelegraphicTransferBean(
                "终端名称展示在这里",
                "已通过",
                "霍辉",
                "2025-8-18 19:30",
                "FH202508130002",
                "电汇",
                "+¥40.00"
        ));

        transferList.add(new TelegraphicTransferBean(
                "终端名称展示在这里",
                "已作废",
                "霍辉",
                "2025-8-18 19:30",
                "FH202508130002",
                "电汇",
                "+¥40.00"
        ));

        adapter.setData(transferList);
        updateEmptyView();
    }

    /**
     * 更新空数据视图显示状态
     */
    private void updateEmptyView() {
        if (transferList == null || transferList.isEmpty()) {
            emptyView.show();
            rvTransferList.setVisibility(View.GONE);
        } else {
            emptyView.hide();
            rvTransferList.setVisibility(View.VISIBLE);
        }
    }

    /**
     * {@link OnRefreshLoadMoreListener}
     */
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
            // 模拟刷新数据
            loadMockData();
            srlRefresh.finishRefresh();
            toast("刷新成功");
        }, 1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
            // 模拟加载更多数据
            int currentSize = transferList.size();
            for (int i = 0; i < 3; i++) {
                transferList.add(new TelegraphicTransferBean(
                        "终端名称" + (currentSize + i + 1),
                        "待审批",
                        "霍辉",
                        "2025-8-18 19:30",
                        "FH20250813000" + (currentSize + i + 1),
                        "电汇",
                        "+¥40.00"
                ));
            }
            adapter.setData(transferList);
            srlRefresh.finishLoadMore();
            toast("加载成功");

            // 模拟最多加载到30条
            if (transferList.size() >= 30) {
                adapter.setLastPage(true);
                srlRefresh.setNoMoreData(true);
            }
        }, 1000);
    }

    /**
     * {@link BaseAdapter.OnItemClickListener}
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        TelegraphicTransferBean item = adapter.getItem(position);
        // TODO: 跳转到电汇收款详情页面
        TelegraphicDetailsActivity.start(getActivity(),item.getDocumentNo());
    }
}

